package com.applet.mallappletbackstage.service.impl;

import com.applet.mallappletbackstage.mapper.IntegralDao;
import com.applet.mallappletbackstage.entity.Integral;
import com.applet.mallappletbackstage.service.IntegralService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Integral)表服务实现类
 *
 * @author makejava
 * @since 2020-08-14 16:40:45
 */
@Service("integralService")
public class IntegralServiceImpl extends ServiceImpl<IntegralDao, Integral> implements IntegralService {

    @Autowired
    IntegralDao integralDao;

    @Override
    public void addIntegral(String user) {
        Integral integral = new Integral();
        integral.setCategory("1");
        integral.setUser(user);
        integral.setValue(2);
        integral.setOrigin("注册");
        baseMapper.insert(integral);
    }

    public Integer selectSum(String id) {
        QueryWrapper<Integral> integralQueryWrapper = new QueryWrapper<>();
        integralQueryWrapper.and(integralQueryWrapper1 ->
                integralQueryWrapper1.eq("user", id)
        );
        List<Integral> integrals = this.integralDao.selectList(integralQueryWrapper);
        Integer add = (Integer) integrals.stream().filter((integral) -> {
            return "1".equals(integral.getCategory());
        }).map(Integral::getValue).reduce(Integer::sum).orElse(0);
        Integer reduce = (Integer) integrals.stream().filter((integral) -> {
            return "0".equals(integral.getCategory());
        }).map(Integral::getValue).reduce(Integer::sum).orElse(0);
        return add - reduce;
    }

    public Integral getStates(String user) {
        return this.integralDao.getStatus(user);
    }
}
